package com.neu.util;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

//@Component
public class SmsUtil {
    //@Value("http://utf8.api.smschinese.cn")
    private String url;
    //@Value("wfccpa")
    private String uid;
    //@Value("02a2a8240660a9d61badda")
    private String key;

    public String sendSms(String to, String content) {

        try {
            HttpClient client = new HttpClient();
            PostMethod post = new PostMethod(url);
            post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");// 在头文件中设置转码
            NameValuePair[] data = { new NameValuePair("Uid", uid),
                    new NameValuePair("Key", key), new NameValuePair("smsMob", to),
                    new NameValuePair("smsText", content) };
            post.setRequestBody(data);

            client.executeMethod(post);

            String result = new String(post.getResponseBodyAsString().getBytes("utf-8"));
            System.out.println(result); // 打印返回内容
            post.releaseConnection();
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }

    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

}

